export default {
	async getQuestionList(params) {
		console.log('打印请求参数')
		let _push_form = {}
		console.log(params)
		if (params.type == 'public') {
			_push_form.partId = params.rootPartId
		} else {
			_push_form.partId = params.rootPartId
		}
		_push_form.estateId = params.estateId
		_push_form.queryEndTime = params.queryEndTime
		_push_form.queryStartTime = params.queryStartTime
		_push_form.statusList = params.statusList
		_push_form.issuesType = params.issuesType

		const {
			data
		} = await uni.$http.post(uni.$http.baseURL + '/qualityIssue/getQualityList', _push_form)
		if (data.status !== 0) {
			uni.$showMsg(data.message)
			return []

		} else {
			if (data.body.length == 0) {
				// uni.$showMsg('暂无数据！')
				return []
			} else {
				return data.body
			}
		}
	},
	async getQuestionEchart(params) {
		console.log(params)
		let _push_form = {}
		_push_form.estateId = params.estateId
		// _push_form.rootPartId = params.rootPartId
		_push_form.issueType = params.issuesType
		const {
			data
		} = await uni.$http.post(uni.$http.baseURL + '/qualityIssue/getQualityChart', _push_form)
		if (data.status !== 0) {
			return uni.$showMsg(data.message)
		} else {
			return data.body
		}
	},
	async getQuestionDetailsById(id) {
		let _push_form = {}
		_push_form.qualityId = id
		const data = await uni.$H.post('/qualityIssue/getQualityIssueById', _push_form, {
			authCode: true
		})
		if (data.status !== 0) {
			return uni.$showMsg(data.message)
		} else {
			return data.body
		}
	},
	async getQuestionChild(para) {
		let _push_form = {}
		_push_form.parentId = para.parentId
		_push_form.type = para.type
		const data = await uni.$H.get('/faultCategory/getByParentId', _push_form, {
			authCode: true
		})
		if (data.status !== 0) {
			uni.$showMsg(data.message)
			return []
		} else {
			return data.body
		}
	},
	access() {
		let cachePath = wx.env.USER_DATA_PATH + "/cache";
		return new Promise(function(resolve, reject) {
			let fm = wx.getFileSystemManager();
			fm.access({
				path: cachePath,
				success: function(res) {
					resolve();
				},
				fail: function(err) {
					resolve(err);
				}
			});
		});
	},
	mkdir() {
		let cachePath = wx.env.USER_DATA_PATH + "/cache";
		return new Promise(function(resolve, reject) {
			let fm = wx.getFileSystemManager();
			fm.mkdir({
				dirPath: cachePath,
				recursive: true,
				success: function(res) {
					resolve();
				},
				fail: function(err) {
					resolve(err);
				}
			});
		});
	},
	async downloadAndSave(_url) {
		let check = this.access()
		console.log(check)
		let mkdir = this.mkdir()
		const downloadTask = wx.downloadFile({
			url: _url, //仅为示例，并非真实的资源
			success: (res) => {
				var filePath = res.tempFilePath;
				var _filePath = wx.env.USER_DATA_PATH + '/' + uni.$u.guid(10) + '.xlsx'

				const FileSystemManager = wx.getFileSystemManager()
				FileSystemManager.saveFile({ //下载成功后保存到本地
					tempFilePath: res.tempFilePath,
					filePath: _filePath,
					success(res2) {
						console.log(res2)
						console.log(wx.env.USER_DATA_PATH)
						if (res2.errMsg == 'saveFile:ok') {
							uni.showToast({
								title: '文件保存至' + _filePath,
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '文件下载报错了',
								icon: 'none'
							})
						}
					},
					fail() {
						uni.showToast({
							title: '文件下载报错了',
							icon: 'none'
						})
					}
				})
			}
		});
		downloadTask.onProgressUpdate((res) => {
			console.log('下载进度' + res.progress);
			console.log('已经下载的数据长度' + res.totalBytesWritten);
			console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
		});
	}
}
